package com.s69.demo.controller;

import com.github.pagehelper.PageInfo;
import com.s69.demo.dto.AppInfoDto;
import com.s69.demo.entity.AppInfo;
import com.s69.demo.entity.DevUser;
import com.s69.demo.service.AppInfoService;
import com.s69.demo.utils.SessionUtils;
import com.s69.demo.vo.ResultVo;
import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.UUID;

@RestController
@RequestMapping("/appInfo")
public class AppInfoController {

    @Autowired
    AppInfoService appInfoService;

    @PostMapping("/page")
    public ResultVo page(@RequestBody AppInfoDto appInfoDto,
                         @RequestParam(defaultValue = "1") Integer pageNum,
                         HttpServletRequest request) {
        String token = request.getHeader("token");
        String[] split = token.split("-");
        if (split[3].equals("admin")) {
            appInfoDto.setStatus(1L);
        } else {
            appInfoDto.setDevid(Long.valueOf(split[1]));
        }
        PageInfo<AppInfo> pageInfo = appInfoService.getPage(appInfoDto, pageNum);
        return ResultVo.success("", pageInfo);
    }

    @GetMapping("/selectView")
    public ResultVo selectView(Long id) {
        AppInfo appInfo = appInfoService.selectView(id);
        return ResultVo.success("", appInfo);
    }

    @PostMapping("/saveOrUpdate")
    public ResultVo saveOrUpdate(AppInfo appInfo, MultipartFile file, HttpServletRequest request) {
        String token = request.getHeader("token");
        if (file != null && !file.isEmpty()) {
            String originalFilename = file.getOriginalFilename();
            String fileFormat = FilenameUtils.getExtension(originalFilename);
            if (file.getSize() > 500 * 1024) {
                return ResultVo.error("图片大小超过500k");
            } else if (
                    fileFormat.equalsIgnoreCase("jpg") ||
                            fileFormat.equalsIgnoreCase("jpeg") ||
                            fileFormat.equalsIgnoreCase("png") ||
                            fileFormat.equalsIgnoreCase("gif")
            ) {
                String realPath = request.getServletContext().getRealPath("upload/logo");
                File savePath = new File(realPath);
                if (!savePath.exists()) {
                    savePath.mkdirs();
                }
                String fileName = UUID.randomUUID().toString().replace("-", "");
                File saveFile = new File(realPath + "/" + fileName + "." + fileFormat);
                try {
                    file.transferTo(saveFile);
                    appInfo.setLogopicpath("upload/logo/" + fileName + "." + fileFormat);
                } catch (IOException e) {
                    e.printStackTrace();
                    return ResultVo.error("logo图片上传失败");
                }
            } else {
                return ResultVo.error("文件格式不正确");
            }
        }

        DevUser devUser = (DevUser) SessionUtils.get(token);
        if (appInfo.getId() == null) {
            appInfo.setStatus(1L);
            appInfo.setDevid(devUser.getId());
            appInfo.setCreatedby(devUser.getId());
            appInfo.setCreationdate(new Date());
            appInfo.setDownloads(0L);
        } else {
            appInfo.setModifyby(devUser.getId());
            appInfo.setModifydate(new Date());
            appInfo.setStatus(1L);

        }
        appInfoService.saveOrUpdate(appInfo);
        return ResultVo.success("", null);
    }

    @DeleteMapping("/del")
    public ResultVo del(Long id, HttpServletRequest request) {
        try {
            appInfoService.del(id, request);
            return ResultVo.success("", null);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultVo.error("");
        }
    }

    @PutMapping("/sale")
    public ResultVo sale(Long id) {
        try {
            appInfoService.sale(id);
            return ResultVo.success("", null);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultVo.error("");
        }
    }

    @PutMapping("/review/{appid}/{type}")
    public ResultVo review(@PathVariable Long appid,@PathVariable Integer type) {
        try {
            appInfoService.review(appid, type);
            return ResultVo.success("", null);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultVo.error("");
        }
    }

    @PostMapping("/validate")
    public ResultVo validate(@RequestBody AppInfo appInfo) {
        Boolean flag = appInfoService.validate(appInfo);
        if (flag == true) {
            return ResultVo.success("可使用", null);
        } else {
            return ResultVo.error("已存在");
        }

    }

}












